package com.cheyun.netsalev3.view.home


import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.ClueItemGridAdapter
import com.cheyun.netsalev3.adapter.ImageGridAdapter
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.ClueDetails
import com.cheyun.netsalev3.bean.Row
import com.cheyun.netsalev3.databinding.FragmentCustomerItemBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.CustomerItemFragmentViewModel
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 *"待接", "跟进", "订车", "提车", "战败"
 */
class CustomerItemFragment : BaseFragment<FragmentCustomerItemBinding>() {

    override fun initContentView(): Int {
        return R.layout.fragment_customer_item
    }

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            return ViewModelProviders.of(this).get(CustomerItemFragmentViewModel::class.java)
        }
        return null
    }

    private var nkey: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            nkey = it.getString("nkey")
        }
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
        binding.viewModel?.initData(nkey)
    }

    var adapter: MyPagedListAdapter<Row>?=null
    var ownerUid: String = ""

    fun setUid(ownerId:String){
        ownerUid = ownerId
        binding.apply {
            binding.viewModel?.refresh(nkey)
        }
    }

    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.customer_item_layout, binding.viewModel)
        var recyclerView= binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh(nkey)
        }
        
        //通知更新
        LiveEventBus.get()
                .with("CustomerItemFragment", String::class.java)
                .observe(this, Observer<String> {
                    binding.viewModel?.refresh(nkey)
                })
        //通知更新单个
        LiveEventBus.get()
                .with("CustomerItemFragmentSingle", ClueDetails::class.java)
                .observe(this, Observer<ClueDetails> {
//                    binding.viewModel?.refresh(nkey)
                    changeSingleData(it)
                })
    }
    override fun initData() {
        binding.viewModel!!.pagedlist[nkey]?.observe(this, Observer { list ->
            Log.e("pagedlist....", list.toString())
            list?.apply {
                adapter?.submitList(this)
            }
        })
        binding.viewModel!!.hashMap[nkey]?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter?.setNetworkState(it)
        })
    }
    fun changeSingleData(details: ClueDetails){
        binding.viewModel!!.pagedlist[nkey]?.observe(this, Observer { list ->
            list?.apply {
                for (row in this){
                    if(row.tid == details.tid){
                        row.isfocus = details.isfocus
                    }
                }
            }
        })
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == 101) {
            adapter?.notifyDataSetChanged()
        }else if(resultCode == 102){
            var type1HashMap = data?.getSerializableExtra("type1Data") as HashMap<String, String>
            var type2HashMap = data?.getSerializableExtra("type2Data") as HashMap<String, String>
            var type3HashMap = data?.getSerializableExtra("type3Data") as HashMap<String, String>
//            var type4HashMap = data?.getSerializableExtra("type4Data") as HashMap<String, String>
            var filters = HashMap<String, String>()
            if (type1HashMap.size > 0)
                filters.putAll(type1HashMap)
            if (type2HashMap.size > 0)
                filters.putAll(type2HashMap)
            if (type3HashMap.size > 0)
                filters.putAll(type3HashMap)
//            if (type4HashMap.size > 0)
//                filters.putAll(type4HashMap)
            if(data?.hasExtra("sort")){
                var sort = data?.getStringExtra("sort")
                filters.put("sort",sort)
            }
            if(data?.hasExtra("order")){
                var order = data?.getStringExtra("order")
                filters.put("order",order)
            }
            binding.viewModel?.refresh(nkey, filters)
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(nkey: String) =
                CustomerItemFragment().apply {
                    arguments = Bundle().apply {
                        putString("nkey", nkey)
                    }
                }
    }
}
